package com.senven.shoppingcar.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.senven.common.entity.ShoppingTrolley;
import com.senven.shoppingcar.service.IShoppingTrolleyService;
import com.senven.shoppingcar.vo.CommonResult;
import com.senven.shoppingcar.vo.ShopingCarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author zsy
 * @Date 2022/9/3 9:13
 * @PackageName:com.senven.shoppingcar.controller.api
 * @ClassName: ApiShoppingTrolleyController
 * @Version 1.0
 */

@RestController
@RequestMapping("/api/shoppingcar/shoppingTrolley")
public class ApiShoppingTrolleyController {

    @Autowired
    private IShoppingTrolleyService trolleyService;
    @Autowired
    private StringRedisTemplate redisTemplate;


//添加进购物车
    @PostMapping("/addCat")
    public CommonResult addCat(Integer branchid,Integer goodsid,Integer stNum,Integer goodsPrice,HttpServletRequest request){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String uid = user.getString("id");
        return trolleyService.addCat(branchid,goodsid,stNum,goodsPrice,uid);
    }
//    从购物车中删除
    @GetMapping("/delCat")
    public CommonResult delCat( Integer ctid){
        return trolleyService.delCat(ctid);
    }

    @GetMapping("/addNum")
    public Integer addNum(Integer gid,Integer bid){
        return trolleyService.updateApiAddNum(gid,bid);
    }

    @GetMapping("/reduceNum")
    public Integer reduceNum(Integer gid,Integer bid){
        return trolleyService.updateApiReduceNumNum(gid,bid);
    }
}
